import { DownloadOutlined, PaperClipOutlined, UploadOutlined } from '@ant-design/icons';
import { Space } from 'antd';
import { AttachmentButton, Upload, UploadProps } from '../../../../components';
import { ITableAttachmentProps } from '../../../../interface';

interface AttachmentToolbarProps extends UploadProps {
  permission: ITableAttachmentProps['permission'];
  isReady?: boolean;
  simpleUpload: boolean;
  count: number;
  uploading?: boolean;
  zipDownload: () => Promise<void>;
  openAttachment: () => Promise<any>;
  uploadRef: any;
}
export function AttachmentToolbar(props: AttachmentToolbarProps) {
  const {
    permission,
    disabled,
    isReady = true,
    simpleUpload,
    count,
    uploading = false,
    zipDownload,
    openAttachment,
    uploadRef,
    ...rest
  } = props;
  return (
    <Space size={[15, 10]} wrap>
      <Upload {...rest} ref={uploadRef}>
        <AttachmentButton
          permissionStatus={permission?.add}
          disabled={disabled || !isReady || uploading}
          loading={uploading}
          icon={<UploadOutlined />}
        >
          上传文件
        </AttachmentButton>
      </Upload>
      {!simpleUpload && (
        <AttachmentButton
          disabled={!isReady}
          permissionStatus={permission?.zipDownload}
          icon={<DownloadOutlined />}
          onClick={zipDownload}
        >
          打包下载
        </AttachmentButton>
      )}
      {!simpleUpload && (
        <AttachmentButton
          disabled={!isReady}
          permissionStatus={(permission as any)?.manage}
          icon={<PaperClipOutlined />}
          onClick={openAttachment}
        >
          附件管理{count > 0 ? ` (${count})` : ''}
        </AttachmentButton>
      )}
    </Space>
  );
}
